package com.example.controller;

import com.example.pojo.Student;
import com.example.pojo.TableData;
import com.example.server.StudentServer;
import com.example.util.DateUtils;
import com.github.pagehelper.PageInfo;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.beans.Visibility;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
//实现跨域注解
//origin="*"代表所有域名都可访问
//maxAge飞行前响应的缓存持续时间的最大年龄，简单来说就是Cookie的有效期 单位为秒
//若maxAge是负数,则代表为临时Cookie,不会被持久化,Cookie信息保存在浏览器内存中,浏览器关闭
@CrossOrigin(origins = "*",maxAge = 3600)
public class Studentcontrller {
    //文件上传的
    @Value("${file.upload.path}")
    private String fiepath;
    @Autowired
    private StudentServer studentServer;
    /*
    搜索加分页
     */
    @RequestMapping("/qureyall")
    @ResponseBody
    public TableData StudentQueryall(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "limit",defaultValue = "4") Integer pageSize,
                                     String name){
        PageInfo<Student> querall = studentServer.querall(pageNum, pageSize, name);
        Map<String, Object> map=new HashMap<>();
      TableData  tableData = new TableData();

        tableData.setCode(0);//
        tableData.setMsg("成功");//执行成功返回“成功”
        tableData.setCount(querall.getTotal());//设置总条数
        tableData.setData(querall.getList());//设置当前数据

        return tableData;
    }
    /*
    删除
     */
    @PostMapping("/delet")
    @ResponseBody
    public int Delerbyid(@RequestBody Student student) throws NoSuchFieldException {
        int i = studentServer.Deleteaccordingtoid(student.getId());
        return i;
    }
    /*
    修改
     */
    @ResponseBody
    @RequestMapping("/change")
    public int Change(Student student){
        int changethedata = studentServer.changethedata(student);
        return changethedata;
    }
    /*
    增加数据
     */
//    @RequestMapping("/incace")
//    @ResponseBody
//    public int Increase(Student student){
//        int increasedata = studentServer.Increasedata(student);
//        return increasedata;
//    }
    @PostMapping("/incace")
    @ResponseBody
    public int add(@RequestParam(value ="gender")MultipartFile gender, @RequestParam(value = "name") String name, @RequestParam(value = "specialty") String specialty, @RequestParam(value = "birthday") String birthday, @RequestParam(value = "age")int age) {
        System.out.println(name +"     "+specialty);
        Date date = DateUtils.createDate(birthday);

        String locatoll="http://localhost:8080/imgs/";
        if (!gender.isEmpty()) {
            //不是空进行获取
            // 获取源文件的名称
            String fileName =gender.getOriginalFilename();            //获取源文件的后缀名字
            String suffix = FilenameUtils.getExtension(fileName);            //使用uuid重命令 文件的名称
            String newFileName = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
            //使用日期解决同一个文件夹文件过多
            String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                     //组装最终的文件名
            Student student=new Student();
            student.setAge(age);
            student.setBirthday(date);

            student.setSpecialty(specialty);
            student.setName(name);
            String finalName = datePath + "/" + newFileName;
            //2020-11-17/1c1da80cf4484135917e130107c6928d.jpg
            student.setGender(locatoll+finalName);
            File dest = new File(fiepath + finalName);
            if (!dest.getParentFile().exists()) {//判断有没有这个文件夹
                dest.getParentFile().mkdirs();//创建文件夹
            }
            try {
                gender.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }

            int increasedata = studentServer.Increasedata(student);
             if (increasedata==1){
                 return increasedata;
             }else {
                 return 0;
             }
        }else {
            return 0;
        }




    }
    @GetMapping("/bbb")
    @ResponseBody
    public List<Student>  getStukk(){
        return studentServer.Queryall();
    }


}
